package com.leon.alishop.spider.bucket;

import java.util.List;
import java.util.Vector;
import java.util.concurrent.atomic.AtomicInteger;

import com.leon.alishop.model.ProviderInfo;

public class ProviderBucket {

	private final Vector<ProviderInfo> providerCollection = new Vector<ProviderInfo>();
	
	private final AtomicInteger pos = new AtomicInteger();
	
	private volatile int size;
	
	public ProviderBucket(List<ProviderInfo> untreatedProviderList){
		if(untreatedProviderList != null){
			providerCollection.addAll(untreatedProviderList);
			size = untreatedProviderList.size();
		}
	}
	
	public ProviderInfo nextProviderInfo(){
		int now = pos.intValue();
		if(now >= size)
			return null;
		pos.incrementAndGet();
		return providerCollection.get(now);
	}
	
	public void reset(){
		pos.set(0);
	}
	
	public void setPosToProviderId(Integer providerId){
		if(null != providerId)
		for (int i = 0,len = providerCollection.size(); i < len; i++) {
			if(providerCollection.get(i).getId().equals(providerId)){
				pos.set(i);
				return;
			}
		}
	}
	
}
